'''
Created on Feb 15, 2011

@author: oabalbin
'''
from collections import defaultdict

def intersect_quality_files(filea, fileb, outfile):
    
    first_array=defaultdict()
    filea.next()
    for l in filea:
        fields=l.strip('\n').split('\t')
        first_array[fields[0]+'_'+fields[1]]=fields[2]
    
    fileb.next()
    second_array=defaultdict()
    for m in fileb:
        fields=m.strip('\n').split('\t')
        second_array[fields[0]+'_'+fields[1]]=fields[2]
        
    common_pos=set(first_array.keys()).intersection(set(second_array.keys()))
    print len(common_pos)
    
    for key in common_pos:
        qualA=first_array[key]
        qualB=second_array[key]
        outfile.write(key.replace('_','\t')+'\t'+qualA+'\t'+qualB+'\n')
        
fileA=open('/exds/users/oabalbin/projects/snps/exomes/FMPN_80/analysis/isec_calls/FMPN.gatk_snps.t.raw.knownSNPs.isec.SNPs.isec.lqual')
fileB=open('/exds/users/oabalbin/projects/snps/exomes/FMPN_80/analysis/isec_calls/FMPNtumor.sam_snps.mpileup.knownSNPs.isec.SNPs.isec.lqual')
outputfile= open('/exds/users/oabalbin/projects/snps/exomes/FMPN_80/analysis/isec_calls/FMPN.t.common.knownSNPs.isec.SNPs.isec.lqual','w')   

intersect_quality_files(fileA, fileB, outputfile)